/*
 * @Author: Jiraiya
 * @Date: 2020-03-03 16:07:30
 * @LastEditors: Jiraiya
 * @LastEditTime: 2020-03-03 20:33:21
 * @Description: 
 */

 //🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏

 const [s1, s2] = [Symbol('foo'), Symbol('foo')]
 console.log(s1 === s2) // false

 // 返回描述符
 console.log(s1.description) // foo

 // 全局注册symbol 与 作用域无关
 const [s3, s4] = [Symbol.for('cat'), Symbol.for('cat')]
 console.log(s3 === s4)

 //是否全局注册
 console.log(Symbol.keyFor(s1), Symbol.keyFor(s3))
 // undefined 'cat'

 
 //🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏
 //内部方法


// 1. instanceof

class MyClass {
    [Symbol.hasInstance](foo){
        return foo instanceof Array;
    }
}
console.log([] instanceof new MyClass()) //true


// 2. 数组concat是否展开

let arr = [1, 2, 3]
console.log(['a'].concat(arr, 'b')) // [ 'a', 1, 2, 3, 'b' ]
arr[Symbol.isConcatSpreadable] = false;
console.log(['a'].concat(arr, 'b')) // [ 'a', [1, 2, 3], 'b' ]

// 3. species
class MyArray extends Array{}
const arr = new MyArray(1, 2, 3)
const arr1 = arr.map(x=>x)
console.log(arr instanceof MyArray, arr1 instanceof MyArray) // true true


class MyArray extends Array{
    static get [Symbol.species](){ return Array }
}
const arr = new MyArray(1, 2, 3)
const arr1 = arr.map(x=>x)
console.log(arr instanceof MyArray, arr1 instanceof MyArray) // true false

// 4. Regexp.prototype[Symbol.match]
RegExp.prototype[Symbol.match] = function(){
    return 'xx'
}
console.log('a1'.match(/\d+/g)) //xx

// 5. Regexp.prototype[Symbol.replace]
// 6. Regexp.prototype[Symbol.split]
// 7. Regexp.prototype[Symbol.search]

// 8. Symbol.iterator
// 9. Symbol.toPrimitive
// 10. Symbol.toStringTag
const obj = {
    [Symbol.iterator]: function *(){
        yield 1;
        yield 2;
        yield 3;
    },
    [Symbol.toPrimitive](){
        return 'xx'
    },
    [Symbol.toStringTag]: '类型'
}

console.log([...obj]) //[1, 2, 3]
console.log(String(obj))// xx
console.log(obj.toString()) //[object 类型]

// 11. Symbol.unscopables